package com.example.dangdangbackend.service.impl;

import com.example.dangdangbackend.DTO.*;
import com.example.dangdangbackend.VO.BookTableVO;
import com.example.dangdangbackend.VO.SelectBooksVO;
import com.example.dangdangbackend.common.CustomException;
import com.example.dangdangbackend.entity.Book;
import com.example.dangdangbackend.mapper.BookMapper;
import com.example.dangdangbackend.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    /**
     * 进行插入书目操作
     * @param bookDTO
     */
    @Override
    public void insertBook(BookDTO bookDTO) {
        if(bookDTO==null){
            throw new CustomException("插入书目为空");
        }

        Book book = new Book();
        book.setBookName(bookDTO.getBookName());
        book.setCategoryId(bookDTO.getCategoryId());
        book.setOriginPrice(BigDecimal.valueOf(bookDTO.getOriginPrice()));
        book.setDangdangPrice(BigDecimal.valueOf(bookDTO.getDangdangPrice()));
        book.setStocks(bookDTO.getStocks());
        book.setWriter(bookDTO.getWriter());
        book.setPublisher(bookDTO.getPublisher());
        book.setPublishTime(bookDTO.getPublishTime());
        book.setOrderEdition(bookDTO.getOrderEdition());
        book.setPrintTime(bookDTO.getPrintTime());
        book.setPrintNum(bookDTO.getPrintNum());
        book.setIBSN(bookDTO.getIBSN());
        book.setFormat(bookDTO.getFormat());
        book.setPaper(bookDTO.getPaper());
        book.setBookPackage(bookDTO.getBookPackage());
        book.setPageNum(bookDTO.getPageNum());
        book.setWordNum(bookDTO.getWordNum());
        book.setPicture(bookDTO.getPicture());
        book.setEditorPromote(bookDTO.getEditorPromote());
        book.setContentIntroduction(bookDTO.getContentIntroduction());
        book.setWriterIntroduction(bookDTO.getWriterIntroduction());
        book.setBasicDirection(bookDTO.getBasicDirection());
        book.setMediaRemark(bookDTO.getMediaRemark());

        String currentTimeString = LocalDateTime.now().format(formatter);
        LocalDateTime time = LocalDateTime.parse(currentTimeString, formatter);
        book.setCreateTime(time);
        book.setUpdateTime(time);
        bookMapper.insertBook(book);
    }

    /**
     * 分页搜寻
     * @param bookSearchDTO
     * @return
     */
    @Override
    public PageInfo<BookTableVO> searchBooksLike(BookSearchDTO bookSearchDTO) {
        PageHelper.startPage(bookSearchDTO.getPageNum(),bookSearchDTO.getPageSize());
        List<BookTableVO> list = bookMapper.searchBooks(bookSearchDTO);

        PageInfo<BookTableVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 修改
     * @param updateBookDTO
     */
    @Override
    public void updateBookById(UpdateBookDTO updateBookDTO) {

        bookMapper.updateBookById(updateBookDTO);
    }

    /**
     * 查，先查再改
     * @param id
     * @return
     */
    @Override
    public BookDTO selectById(int id) {
        return bookMapper.selectById(id);
    }

    /**
     * 批量删除操作
     * @param ids
     */
    @Override
    public void deleteIds(List<Integer> ids) {
        bookMapper.deleteBooks(ids);
    }

    @Override
    public PageInfo<BookTableVO> searchBookByCategory(CategorySearchDTO categorySearchDTO) {
        PageHelper.startPage(categorySearchDTO.getPageNum(),categorySearchDTO.getPageSize());
        List<BookTableVO> bookTableVOS = bookMapper.searchBooksByCategory(categorySearchDTO);
        PageInfo<BookTableVO> pageInfo = new PageInfo<>(bookTableVOS);

        return pageInfo;

    }


    //对sales进行增加操作
    @Override
    public void addBookSales(List<AddBookSaleDTO> addBookSaleDTOS) {
        bookMapper.addBookSales(addBookSaleDTOS);
    }

    @Override
    public void alertStatus(int bookId) {
        bookMapper.alertStatus(bookId);
    }

    @Override
    public List<SelectBooksVO> getRecommendBooks() {
        return bookMapper.getRecommendBooks();
    }

    @Override
    public List<SelectBooksVO> selectSalesMost() {
        return bookMapper.selectSalesMost();
    }

    @Override
    public List<SelectBooksVO> selectRecentBooks() {
        return bookMapper.selectRecentBooks();
    }

    @Override
    public List<SelectBooksVO> selectNewBookAndSalesMost() {
        return bookMapper.selectNewBookAndSalesMost();
    }

}
